package cn.edu.huas.shopping.controller;

import cn.edu.huas.shopping.dao.ProductDao;
import cn.edu.huas.shopping.dao.ProductDaoImpl;
import cn.edu.huas.shopping.pojo.OrderItem;
import cn.edu.huas.shopping.pojo.PageResult;
import cn.edu.huas.shopping.pojo.Product;
import cn.edu.huas.shopping.pojo.ResponseResult;
import cn.edu.huas.shopping.service.ProductService;
import cn.edu.huas.shopping.service.ProductServiceImpl;
import cn.edu.huas.shopping.utils.JsonUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebInitParam;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@WebServlet(value = "*.product",initParams = {@WebInitParam(name="pageSize",value = "4")})
public class ProductMangerAction extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        ProductService productService=new ProductServiceImpl();
        ProductDao productDao=new ProductDaoImpl();
        ResponseResult<Map<String,Object>> result=new ResponseResult<>();
        String matchValue = request.getHttpServletMapping().getMatchValue();


        if ("index".equals(matchValue)) {
            //获得首页的商品信息
            Map<String, Object> maps=productService.loadIndexProduct();
            result.setFlag(true);
            result.setCode(20031);
            result.setData(maps);
            result.setMessage("ok");
            out.println(JsonUtils.objectToJson(result));
        }else if("queryid".equals(matchValue)){
            //根据id获取商品信息
            Integer id= Integer.valueOf(request.getParameter("id"));
            Product oneProduct=productService.loadOneProduct(id);
            ResponseResult result1=new ResponseResult<>(true,"ok",20031,oneProduct);
            out.println(JsonUtils.objectToJson(result1));
            //System.out.println(JsonUtils.objectToJson(result1));
        }else if ("orderPostId".equals(matchValue)){
            //获取orderitem中的所有数据
            List<OrderItem> orderItemList=new ArrayList<>();
            OrderItem orderItem;
            Integer id= Integer.valueOf(request.getParameter("id"));
            do {
                orderItem=productService.loadOrderItem(id);
                if(Objects.nonNull(orderItem)){
                    orderItemList.add(orderItem);
                }
                id++;
            }while (Objects.nonNull(orderItem));
            ResponseResult result2=new ResponseResult<>(true,"ok",20031,orderItemList);
            out.println(JsonUtils.objectToJson(result2));
        }else if ("page".equals(matchValue)){
            String cidStr=request.getParameter("cid");
            Integer cid=Integer.parseInt(cidStr.trim());

            String pageIndexStr=request.getParameter("pageIndex");
            Integer pageIndex=pageIndexStr==null?1:Integer.parseInt(pageIndexStr.trim());

            //获得配置参数中的数据
            String pageSizeStr=getServletConfig().getInitParameter("pageSize");
            Integer pageSize=Integer.parseInt(pageSizeStr.trim());

            PageResult<Product> pageResult = productService.pageQuery(cid, pageIndex, pageSize);
            //System.out.println(JsonUtils.objectToJson(pageResult));
            out.println(JsonUtils.objectToJson(pageResult));
        }

    }
}
